program ChromeParseBlame;

include <core.vhs>

import core::*;
import core::Pattern::*;
import core::Logging::*;



function main(input='blame.xml', output=format('output/gc-blame-%s.csv', version), version='1.0') {	
	var out = new CsvFile(output);
	var rvMap = map [];
	var rvDate = map [];
	parseSvnBlameFile(input = new SequenceStream(input, 4096), revisionMap=rvMap, revisionDate=rvDate);
	for(var key : rvMap.keySet())
		out.writeCsvLine([version, key, rvMap.get(key), rvDate.get(key)]);
	out.close();
}

pattern parseSvnBlameFile($revisionMap, $revisionDate =  null, $maxOccur = INFINITY, $minOccur = 0) {
	locate('<entry');
	locate_end('</entry>');
	var rv = BlameRevision();
	if (revisionMap.containsKey(rv))
		revisionMap.put(rv, 1 + revisionMap[rv]);
	else {
		revisionMap.put(rv, 1);
		revisionDate?.put(rv, BlameDate());
	}
}

pattern<DataPart> BlameRevision($dataPattern=@'revision="(\d+)"') this[1]
pattern<DataPart> BlameDate($dataPattern=@'<date>(\d+-\d+-\d+)') this[1]